package com.xaicode.template.gendata.mall.model;

import com.xaicode.template.gendata.mall.common.RandomNum;
import com.xaicode.template.gendata.mall.common.RandomOptionGroup;
import com.xaicode.template.gendata.mall.config.AppConfig;
import com.xaicode.template.gendata.mall.enums.ActionId;
import com.xaicode.template.gendata.mall.enums.ItemType;
import com.xaicode.template.gendata.mall.enums.PageId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Locker xaicode@sina.com
 * @version 1.0
 * @date 2022/5/24 0024 21:41
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AppAction {

    ActionId action_id;
    ItemType item_type;
    String item;

    String extend1;
    String extend2;
    Long ts;

    //AppAction(ActionId.favor_add, appPage.item_type, appPage.item);

    public AppAction(ActionId action_id, ItemType item_type, String item) {
        this.action_id = action_id;
        this.item_type = item_type;
        this.item = item;
    }

    public String toString() {
        return "AppAction(action_id=" + getAction_id() + ", item_type=" + getItem_type() + ", item=" + getItem()
                + ", extend1=" + getExtend1() + ", extend2=" + getExtend2() + ", ts=" + getTs() + ")";
    }

    public static List<AppAction> buildList(AppPage appPage, Long startTs, Integer duringTime) {

        List<AppAction> actionList = new ArrayList<>();
        Boolean ifFavor = RandomOptionGroup.builder()
                .add(Boolean.valueOf(true), AppConfig.if_favor_rate.intValue())
                .add(Boolean.valueOf(false), 100 - AppConfig.if_favor_rate.intValue())
                .build().getRandBoolValue();
        Boolean ifCart = RandomOptionGroup.builder()
                .add(Boolean.valueOf(true), AppConfig.if_cart_rate.intValue())
                .add(Boolean.valueOf(false), 100 - AppConfig.if_cart_rate.intValue())
                .build().getRandBoolValue();
        Boolean ifCartAddNum = RandomOptionGroup.builder()
                .add(Boolean.valueOf(true), AppConfig.if_cart_add_num_rate.intValue())
                .add(Boolean.valueOf(false), 100 - AppConfig.if_cart_add_num_rate.intValue())
                .build().getRandBoolValue();
        Boolean ifCartMinusNum = RandomOptionGroup.builder()
                .add(Boolean.valueOf(true), AppConfig.if_cart_minus_num_rate.intValue())
                .add(Boolean.valueOf(false), 100 - AppConfig.if_cart_minus_num_rate.intValue())
                .build().getRandBoolValue();
        Boolean ifCartRm = RandomOptionGroup.builder()
                .add(Boolean.valueOf(true), AppConfig.if_cart_rm_rate.intValue())
                .add(Boolean.valueOf(false), 100 - AppConfig.if_cart_rm_rate.intValue())
                .build().getRandBoolValue();
        Boolean ifGetCouponRm = RandomOptionGroup.builder()
                .add(Boolean.valueOf(true), AppConfig.if_get_coupon.intValue())
                .add(Boolean.valueOf(false), 100 - AppConfig.if_get_coupon.intValue())
                .build().getRandBoolValue();

        if (appPage.page_id == PageId.good_detail) {

            if (ifFavor) {
                AppAction favorAction = new AppAction(ActionId.favor_add, appPage.item_type, appPage.item);
                actionList.add(favorAction);
            }
            if (ifCart) {
                AppAction cartAction = new AppAction(ActionId.cart_add, appPage.item_type, appPage.item);
                actionList.add(cartAction);
            }
            if (ifGetCouponRm) {
                int couponId = RandomNum.getRandInt(1, AppConfig.max_coupon_id.intValue());
                AppAction couponAction = new AppAction(ActionId.get_coupon, ItemType.coupon_id, String.valueOf(couponId));
                actionList.add(couponAction);
            }
        } else if (appPage.page_id == PageId.cart) {

            if (ifCartAddNum) {
                int skuId = RandomNum.getRandInt(1, AppConfig.max_sku_id.intValue());
                AppAction favorAction = new AppAction(ActionId.cart_add_num, ItemType.sku_id, skuId + "");
                actionList.add(favorAction);
            }
            if (ifCartMinusNum) {
                int skuId = RandomNum.getRandInt(1, AppConfig.max_sku_id.intValue());
                AppAction favorAction = new AppAction(ActionId.cart_minus_num, ItemType.sku_id, skuId + "");
                actionList.add(favorAction);
            }
            if (ifCartRm) {
                int skuId = RandomNum.getRandInt(1, AppConfig.max_sku_id.intValue());
                AppAction favorAction = new AppAction(ActionId.cart_remove, ItemType.sku_id, skuId + "");
                actionList.add(favorAction);
            }
        } else if (appPage.page_id == PageId.trade) {
            Boolean ifAddAddress = RandomOptionGroup.builder()
                    .add(Boolean.valueOf(true), AppConfig.if_add_address.intValue())
                    .add(Boolean.valueOf(false), 100 - AppConfig.if_add_address.intValue())
                    .build().getRandBoolValue();
            if (ifAddAddress) {
                AppAction appAction = new AppAction(ActionId.trade_add_address, null, null);
                actionList.add(appAction);
            }
        } else if (appPage.page_id == PageId.favor) {
            Boolean ifFavorCancel = RandomOptionGroup.builder()
                    .add(Boolean.valueOf(true), AppConfig.if_favor_cancel_rate.intValue())
                    .add(Boolean.valueOf(false), 100 - AppConfig.if_favor_cancel_rate.intValue())
                    .build().getRandBoolValue();
            int skuId = RandomNum.getRandInt(1, AppConfig.max_sku_id);
            for (int i = 0; i < 3; i++) {
                if (ifFavorCancel) {
                    AppAction appAction = new AppAction(ActionId.favor_canel, ItemType.sku_id, (skuId + i) + "");
                    actionList.add(appAction);
                }
            }
        }

        int size = actionList.size();
        long avgActionTime = (duringTime / (size + 1));
        for (int i = 1; i <= actionList.size(); i++) {
            AppAction appAction = actionList.get(i - 1);
            appAction.setTs(startTs + i * avgActionTime);
        }
        return actionList;
    }

}
